home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection Student Program / ADC Tools Sampler CD Disk 3 1999.iso / Metrowerks CodeWarrior / Java Support / Java_Source / Java2 / src / javax / swing / Autoscroller.java < prev    next >
Encoding:
Java Source  |  1999-05-28  |  2.6 KB  |  103 lines  |  [TEXT/CWIE]

  1. /*
  2.  * @(#)Autoscroller.java    1.7 98/08/26
  3.  *
  4.  * Copyright 1997, 1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package javax.swing;
  16.  
  17. import java.awt.*;
  18. import java.awt.event.*;
  19.  
  20. import java.io.Serializable;
  21. import java.io.ObjectOutputStream;
  22. import java.io.ObjectInputStream;
  23. import java.io.IOException;
  24.  
  25.  
  26. /**
  27.  * @version 1.7 08/26/98
  28.  * @author Dave Moore
  29.  */
  30.  
  31. class Autoscroller extends MouseAdapter implements Serializable
  32. {
  33.     transient MouseEvent event;
  34.     transient Timer timer;
  35.     JComponent component;
  36.  
  37.  
  38.     Autoscroller(JComponent c) {
  39.     if (c == null) {
  40.         throw new IllegalArgumentException("component must be non null");
  41.     }
  42.     component = c;
  43.     timer = new Timer(100, new AutoScrollTimerAction());
  44.     component.addMouseListener(this);
  45.     }
  46.  
  47.     class AutoScrollTimerAction implements ActionListener {
  48.     public void actionPerformed(ActionEvent x) {
  49.         if(!component.isShowing() || (event == null)) {
  50.         stop();
  51.         return;
  52.         }
  53.         Point screenLocation = component.getLocationOnScreen();
  54.         MouseEvent e = new MouseEvent(component, event.getID(),
  55.                       event.getWhen(), event.getModifiers(),
  56.                       event.getX() - screenLocation.x,
  57.                       event.getY() - screenLocation.y,
  58.                       event.getClickCount(), event.isPopupTrigger());
  59.         component.superProcessMouseMotionEvent(e);
  60.     }
  61.     }
  62.  
  63.     void stop() {
  64.     timer.stop();
  65.     event = null;
  66.     }
  67.  
  68.     public void mouseReleased(MouseEvent e) {
  69.     stop();
  70.     }
  71.  
  72.     public void mouseDragged(MouseEvent e) {
  73.     Rectangle visibleRect = component.getVisibleRect();
  74.     boolean contains = visibleRect.contains(e.getX(), e.getY());
  75.  
  76.     if (contains) {
  77.         if (timer.isRunning()) {
  78.         stop();
  79.         }
  80.     } else {
  81.         Point screenLocation = component.getLocationOnScreen();
  82.  
  83.         event = new MouseEvent(component, e.getID(), e.getWhen(), e.getModifiers(),
  84.                    e.getX() + screenLocation.x,
  85.                    e.getY() + screenLocation.y,
  86.                    e.getClickCount(), e.isPopupTrigger());
  87.         if (!timer.isRunning()) {
  88.         timer.start();
  89.         }
  90.     }
  91.     }
  92.  
  93.     private void writeObject(ObjectOutputStream s) throws IOException {
  94.     s.defaultWriteObject();
  95.     }
  96.  
  97.     private void readObject(ObjectInputStream s) throws IOException, ClassNotFoundException 
  98.     {
  99.     s.defaultReadObject();
  100.     timer = new Timer(100, new AutoScrollTimerAction());
  101.     }
  102. }
  103.